program /s10/service_demo.

* View for Measurement Document
class imrg_detail definition inheriting from /s10/any.

  public section.
* table fields for detail view, plus key fields
    data:
      mdocm    type imrg-mdocm, " measurement document
      point    type imrg-point, " Measuring point
      idate    type imrg-idate, " Date
      itime    type imrg-itime, " Measurement time
      mdtxt    type imrg-mdtxt, " Text
      erdat    type imrg-erdat, " Created on
      eruhr    type imrg-eruhr, " Time of day
      ernam    type imrg-ernam, " Created by
      aedat    type imrg-aedat, " Modified on
      aenam    type imrg-aenam, " Changed by
      rootd    type imrg-rootd, " Source document
      readg    type imrg-readg, " Measured value
      recdu    type imrg-recdu, " ME document entry
      cntrr    type imrg-cntrr, " Counter reading
      cdiff    type imrg-cdiff, " Difference
      cdiffi   type imrg-cdiffi, " Contain value
      exchg    type imrg-exchg, " Counter exchange
      prest    type imrg-prest, " Processing status
      mbewertg type imrg-mbewertg. " evaluation

* database table name
    constants:
      dbtablename type string value 'imrg', " for select
      unit_readg  type string value 'recdu',
      unit_cntrr  type string value 'recdu',
      unit_cdiff  type string value 'recdu'.

endclass.

* View for Measurement Document -> List View
class imptt_short definition inheriting from /s10/any.

  public section.

* table fields for list view, plus key fields
    data:
      point          type imptt-point, " measuring point
      pttxt          type imptt-pttxt, " designation
      mptyp          type imptt-mptyp, " measuring point type
      mrngu          type imrc_mrngu, " Unit using text
      recorded_value type rimr0-recdc, " Value using Text

      mdocm          type imrc_mdocm,
      idate          type imrc_idate,
      itime          type imrc_itime,
      rdtot          type imrc_rdtot,
      unitr          type imrc_unitr,
      unitc          type imrc_unitc,
      desir          type imptt-desir, " Target value
      dstxt          type imptt-dstxt, " Text
      dstxtr         type imptt-dstxtr, " Ref.text
      erdat          type imptt-erdat, " Created on
      ernam          type imptt-ernam, " Created by

* HTML Code for the image of the signature
      signature_html type string,

* Object for detail view
      myimrg         type ref to imrg_detail.

    methods:
* Close the view with the measures
      measurement_document_back.

* database table name
    constants:
      dbtablename         type string value 'imptt', " for select
* Units
      unit_recorded_value type string value 'mrngu',
      unit_desir          type string value 'mrngu',
      unit_mrmin          type string value 'mrngu',
      unit_pyear          type string value 'mrngu',
      unit_mrmax          type string value 'mrngu'.

endclass.

* View for Equipment master data -> detail view
class equi_detail definition inheriting from /s10/any.

  public section.

* table fields for detail view, plus key fields
    data:
      equnr    type equi-equnr, " Equipment
      eqtyp    type equi-eqtyp, " Equipment type
      gwlen    type equi-gwlen, " Warranty end date
      gwldt    type equi-gwldt, " Warranty
      baujj    type equi-baujj, " Year of construction
      inbdt    type equi-inbdt, " In operation from
      gwldv    type equi-gwldv, " Warranty
      eqber    type equi-eqber, " Remark
      sernr    type equi-sernr, " Serial number
      kunde    type equi-kunde, " Current customer
      warpl    type equi-warpl, " Maintenance plan

* Table for measure inputs
      tabimptt type table of ref to imptt_short.

* database table name
    constants:
      dbtablename type string value 'equi'. " for select
endclass.

* Equipment master data -> List View
class equi_short definition inheriting from /s10/any.

  public section.

* table fields for list view, plus key fields
    data:
      equnr         type equi-equnr, " Equipment
      eqtyp         type equi-eqtyp, " Type of Equipment using Text
      inbdt         type equi-inbdt, " In operation from
      herst         type equi-herst, " Hersteller
      s10detailview type string.

* database table name
    constants:
       dbtablename type string value 'equi'. " for select

endclass.

class equi_valuehelp definition inheriting from /s10/any.

  public section.

* table fields for list view, plus key fields
    data:
      search_equnr       type equi-equnr, " Equipment
      search_eqtyp       type equi-eqtyp, " Equipmenttyp
      search_herst       type equi-herst, " Hersteller

      search_maxrowcount type string value '100'.

    data:
      testequnr type equi-equnr,
      tabequi   type table of ref to equi_short,
      myequi    type ref to equi_detail.
* valuehelp
    class-data:
       valuehelp_equnr type ref to equi_valuehelp.

    class-methods:
      valuehelp
        returning
          value(selectedequnr) type equi-equnr.

    methods:
      on_enter_list,
      on_detail_tabequi,

      valuehelp_select,
      testdialog,
      valuehelp_exit,
      on_valuehelp_testequnr,

* method to build up tabequi
      build_tabequi
        importing
          search_equnr       type equi-equnr " Equipment
          search_eqtyp       type equi-eqtyp " Equipmenttyp
          search_herst       type equi-herst " Hersteller
          search_maxrowcount type string
        exporting
          tabequi            type table.

endclass.

* Our main class for this application
class equi_manager definition inheriting from /s10/any.

  public section.

* project path (textpool, screens)
    constants:
        projectpath type string value 'sap/bc/bsp/s10/service'.

* Search help
    data: my_equi_valuehelp type ref to equi_valuehelp.

* table fields for list view, plus key fields
    data:
      search_equnr       type equi-equnr, " Equipment
      search_eqtyp       type equi-eqtyp, " Type of Equipment - Dropdown
      search_inbdt       type equi-inbdt, " In operation from
      search_maxrowcount type string value '100'.

    data:
      tabequi        type table of ref to equi_short,
      myequi         type ref to equi_detail,
      tabimptt_input type table of ref to imptt_short.

* Fields for signature
    data:
      mybase64  type string,
      content   type xstring,
      signature type string,

* New measures saved
      saved(1).

    methods:

* Entry point: Show list of equipments
      list,
      on_enter_list,

* User clicked on a row in the table of equipments
      on_detail_tabequi,

* Read details for the selected equipment
      read_detail_tabequi
        importing equnr type equi-equnr,

* Open dialog for entering new measures for the equipment
      open_measurepoints,

* Save measures
      measures_save,

* value-help
      on_valuehelp_search_equnr,


* User clicked on a measurement document link
      on_link_mdocm,

* Close the view for the new measures
      exit_measurepoints_back,

* Save the signature as image in MIME repository
      save_signature
        importing filename type string,


* method to build up tabequi
      build_tabequi
        importing
          search_equnr       type equi-equnr " Equipment
          search_eqtyp       type equi-eqtyp " Equipmenttype
          search_inbdt       type equi-inbdt " In operation from
          search_maxrowcount type string
        exporting
          tabequi            type table.

endclass.


***********************************
* main class for this application
class main definition inheriting from /s10/any.

  public section.

    data: my_equi_manager type ref to equi_manager.

    methods:
      logon.

endclass.

class imrg_detail implementation.

endclass.

class imptt_short implementation.

  method measurement_document_back.

* Just close the dialog
    s10exitdialog(  ).

  endmethod.

endclass.

class equi_detail implementation.

endclass.

class equi_short implementation.

endclass.

* Implementation of our main class
class equi_manager implementation.

  method on_valuehelp_search_equnr.
    data: myequnr type equi-equnr.

* call value help
    myequnr =  equi_valuehelp=>valuehelp( ).

* set selected value
    if myequnr is not initial.
      search_equnr = myequnr.

* return value into "range" field if local value help in range popup
      s10valuehelpreturn( myequnr ).
    endif.

  endmethod.

* display list screen.
  method list.
    s10nextscreen( 'list').
  endmethod.


* User clicked on a measurement document link
  method on_link_mdocm.

    data: mymdocm type imrc_mdocm.

* Get the measurement document for this row
    call method s10fromcontextinfo
      exporting
        key    = 'mdocm'
      changing
        result = mymdocm.

* Create a new object for the measurement document
    data: my_imptt_short type ref to imptt_short.
    create object my_imptt_short.

* Key = measurement document
    my_imptt_short->mdocm = mymdocm.

    data: my_imrg_ba type imrg.

* Read the data for this measurement document
    call function 'MEASUREM_DOCUM_RFC_SINGLE_002'
      exporting
        measurement_document = mymdocm
        with_dialog_screen   = ''
      importing
        imrg_ba              = my_imrg_ba.

    if sy-subrc <> 0.

* Something went wrong
      s10errormessage(
             exporting
               msgid = sy-msgid
               msgno = sy-msgno
               par1  = sy-msgv1
               par2  = sy-msgv2
               par3  = sy-msgv3
                ).
    endif.

* Copy the read data from the function call
    my_imptt_short->s10copy( my_imrg_ba  ).

* We read additional data from the database
    my_imptt_short->s10databaseread( ).


* read details

    if not my_imptt_short->myimrg is bound.
      create object my_imptt_short->myimrg.
    endif.

    my_imptt_short->myimrg->mdocm = my_imrg_ba-mdocm.
    my_imptt_short->myimrg->s10databaseread( ).


* Create the HTML code for the image of the signature
    my_imptt_short->signature_html =  |<img style='padding:5px;border-radius:15px;background-color:white; max-height:200px' src='../../../signatures/|
        && mymdocm && |.png' onerror="this.width=1; this.onerror=null;">|.


* Open the detail view as new dialog
    my_imptt_short->s10dialog( 'details' ).

  endmethod.

  method exit_measurepoints_back.

* Just close the dialog and return to the previous screen
    s10exitdialog( ).

  endmethod.

* We save the measures that the user entered
  method measures_save.

* Already saved?
    if saved is not initial.

      s10errormessage( s10localize( id =  'measure_saved_already' ) ).

    endif.

* Signature?
    if signature is initial.
      s10errormessage( s10localize( id =  'measure_signature_missing' ) ).
    endif.


    data: tabimptt_input_wa type ref to imptt_short.
    data: mymeasurement_document type imrg-mdocm.

* Save measures
    loop at tabimptt_input into tabimptt_input_wa.

      if tabimptt_input_wa->recorded_value is not initial.

        call function 'MEASUREM_DOCUM_RFC_SINGLE_001'
          exporting
            measurement_point    = tabimptt_input_wa->point
            recorded_value       = tabimptt_input_wa->recorded_value
          importing
            measurement_document = mymeasurement_document
          exceptions
            others               = 1.

        if sy-subrc <> 0.

* something went wrong
          s10errormessage(
            exporting
              msgid      =     sy-msgid
              msgno      =     sy-msgno
              par1       =     sy-msgv1
              par2       =     sy-msgv2
              par3       =     sy-msgv3
          ).

        endif.

* Save signature to mime repository
        data: filename type string.
        filename = '/signatures/' && mymeasurement_document && '.png'.
        save_signature( filename = filename ).

        data: mytext type string.
        mytext = s10localize( id =  'measure_saved_for_point').
        replace '%1' in mytext with tabimptt_input_wa->point.

        s10infomessage( mytext ).

* Add CSS class -> input cannot be edited anymore
        tabimptt_input_wa->s10addcss(
          exporting
            attrname     =     'recorded_value'
            cssclassname =     'inputdisabled'
        ).
      endif.

    endloop.

* set saved status
    saved = 'X'.

  endmethod.


* select database values and fill table tabequi
  method build_tabequi.

    data: condition type string,
          join      type string.

    if search_equnr is not initial.
      if condition is not initial.
        condition = condition && | and |.
      endif.
      condition = condition && |EQUNR like '%| && search_equnr && |%'|.
    endif.

    if search_eqtyp is not initial.
      if condition is not initial.
        condition = condition && | and |.
      endif.
      condition = condition && |EQTYP EQ '| && search_eqtyp && |'|.
    endif.

    if search_inbdt is not initial.
      if condition is not initial.
        condition = condition && | and |.
      endif.
      condition = condition && |INBDT >= '| && search_inbdt && |'|.
    endif.

    data: maxrows type i.
    maxrows = search_maxrowcount.

* read data
    s10databaseselect( exporting dbtablename = join condition = condition maxrows = maxrows orderby = 'equnr' changing folder = tabequi ).

  endmethod.

* enter in list screen
* "build" method for table is called automatically
  method on_enter_list.

  endmethod.


* show details in list (line selection)
  method on_detail_tabequi.

* read current table row.
    data: tabindex type i.
    tabindex = s10actionparameter( ).
    read table tabequi index tabindex assigning field-symbol(<row>).

    read_detail_tabequi( <row>->equnr ).

  endmethod.


  method read_detail_tabequi.

* set table key and read detail attributes
    create object myequi.
    myequi->equnr = equnr.
    myequi->s10databaseread( ).

    clear myequi->tabimptt.

    data: myrihimrg type table of rihimrg.
    data: myrihimrg_wa type rihimrg.

    data: myimptt type ref to imptt_short.

    call function 'GET_MEASURING_DOCUMENTS'
      exporting
        equipment  = myequi->equnr
      tables
        et_rihimrg = myrihimrg.


    loop at myrihimrg into myrihimrg_wa.

      create object myimptt.

      myimptt->mdocm = myrihimrg_wa-mdocm.
      myimptt->mrngu = myrihimrg_wa-unitc.
      myimptt->point = myrihimrg_wa-point.
      myimptt->pttxt = myrihimrg_wa-pttxt.
      myimptt->idate = myrihimrg_wa-idate.
      myimptt->itime = myrihimrg_wa-itime.
      myimptt->rdtot = myrihimrg_wa-rdtot.
      myimptt->unitr = myrihimrg_wa-unitr.

      append myimptt to  myequi->tabimptt.

    endloop.

    myequi->s10sort(
      exporting
        foldername =     'tabimptt'
        columns    =     'idate-,itime-'
    ).

  endmethod.


* open measure points for this equipment
  method open_measurepoints.

* Get number of row the user clicked in
    data:
    rownumber   type i.
    rownumber = s10contextinfo( )->rownumber.

* Read the object of this row
    read table tabequi index s10contextinfo( )->rownumber assigning field-symbol(<row>).

    data: mydiimpt type table of diimpt.
    data: mydiimpt_wa type diimpt.



* Get measure points for the equipment in this selected row
    call function 'GET_MEASURING_POINTS_4_EQUIPM'
      exporting
        i_equnr   = <row>->equnr
      tables
        et_diimpt = mydiimpt.

* Copy the data
    data: myimptt type ref to imptt_short.

    clear tabimptt_input.

    loop at mydiimpt into mydiimpt_wa.

      create object myimptt.

      myimptt->mrngu = mydiimpt_wa-unitc.
      myimptt->point = mydiimpt_wa-point.
      myimptt->pttxt = mydiimpt_wa-pttxt.

      append myimptt to  tabimptt_input.

    endloop.

    clear saved.

    s10dialog( 'measurepoints').

* The user might have saved some new measures,
* so update the table
    read_detail_tabequi( <row>->equnr  ).
    <row>->s10detailview = 'X'.


  endmethod.


  method save_signature.

    clear mybase64.
    clear content.

* Image of the signature as Base64 string
    mybase64 = signature.

* Convert to xstring
    call function 'SSFC_BASE64_DECODE'
      exporting
        b64data = mybase64
      importing
        bindata = content
      exceptions
        others  = 8.

* Get API for the MIME repository
    data(o_mime_rep) = cl_mime_repository_api=>get_api( ).

* Get the path of our application as root for the image files
    data: mypath type string.
    mypath = projectpath.

* append the filename
    mypath = mypath && '/' && filename.

* Save image in MIME repository.
* Object catalog entry is created, any existing files are overwritten
*
* -> local object in this case, no transport

    o_mime_rep->put(  i_url = mypath
                     i_content = content
                     i_check_authority = abap_false
                     i_suppress_dialogs = 'X'
                     i_suppress_package_dialog = 'X'
                     i_dev_package = '$TMP'
                     ).
    if sy-subrc <> 0.

* something went wrong
      s10errormessage(
        exporting
          msgid      =     sy-msgid
          msgno      =     sy-msgno
          par1       =     sy-msgv1
          par2       =     sy-msgv2
          par3       =     sy-msgv3
      ).

    endif.

  endmethod.

endclass.


class equi_valuehelp implementation.

* class method value help
  method valuehelp.
    if valuehelp_equnr is initial.
      create object valuehelp_equnr.
    endif.

*   call value help
    selectedequnr = valuehelp_equnr->s10dialog('valuehelp').

  endmethod.
* select database values and fill table tabequi
  method build_tabequi.

    data: condition type string,
          join      type string,
          search    type string.

    if search_equnr is not initial.
      search = search_equnr.
      join = |equi join EQKT on EQKT~EQUNR = equi~equnr and EQKT~SPRAS = 'D' and EQKT~EQKTX like '%| && search_equnr && |%'|.
    endif.

    if search_eqtyp is not initial.
      search = search_eqtyp.
      if condition is not initial.
        condition = condition && | and |.
      endif.
      condition = condition && |EQUI~EQTYP EQ '| && search_eqtyp && |'|.
    endif.

    data: maxrows type i.
    maxrows = search_maxrowcount.

* read data
    s10databaseselect( exporting dbtablename = join condition = condition maxrows = maxrows orderby = 'equi~equnr,equi~eqtyp' changing folder = tabequi ).

endmethod.

* enter in list screen
* "build" method for table is called automatically
  method on_enter_list.

  endmethod.

* show details in list (line selection)
  method on_detail_tabequi.

* read current table row.
    data: tabindex type i.
    tabindex = s10actionparameter( ).
    read table tabequi index tabindex assigning field-symbol(<row>).

* set table key and read detail attributes
    create object myequi.
    myequi->equnr = <row>->equnr.
    myequi->s10databaseread( ).

  endmethod.
* select value
  method valuehelp_select.

    read table tabequi index s10contextinfo( )->rownumber assigning field-symbol(<row>).

*   return keys as dialog parameter
    data: selvalue type string.
    selvalue = <row>->equnr.
    s10exitdialog( selvalue ).

  endmethod.

* leave valuehelp
  method valuehelp_exit.
    s10exitdialog( ).
  endmethod.


* test dialog.
  method testdialog.
    s10nextscreen( 'testdialog').
  endmethod.

  method on_valuehelp_testequnr.
    data: myequnr type equi-equnr.

* call value help
    myequnr =  equi_valuehelp=>valuehelp( ).

* set selected value
    if myequnr is not initial.
      testequnr = myequnr.

* return value into "range" field if local value help in range popup
      s10valuehelpreturn( myequnr ).
    endif.

  endmethod.
endclass.


class main implementation.

* logon user
  method logon.

* set S10 license
    s10setlicense( 'Synactive GmbH demo license number=100 role=S10_USERROLE maxusers=10 signature=955.146.48.30' ).

*  create manager object
    create object my_equi_manager.

* start list display
    my_equi_manager->list( ).

  endmethod.
endclass.